package com.qianying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qianying.comment.CommonResult;
import com.qianying.entity.CustomBrand;
import com.qianying.mapper.CustomBrandMapper;
import com.qianying.service.CustomBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 品牌Service实现类
 *
 * @author check
 * @date 2023-10-20
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomBrandServiceImpl implements CustomBrandService {

    @Autowired
    private CustomBrandMapper customBrandMapper;

    @Override
    public CommonResult<?> save(CustomBrand customBrand) {
        if (customBrand == null) {
            return CommonResult.failed("新增品牌不能为空！");
        }
        customBrand.setId(null);
        if (customBrand.getCustomerId() != null) {
            customBrand.setShopId(null);
        } else if (customBrand.getCustomerId() == null&&customBrand.getShopId() == null) {
            return CommonResult.failed("新增品牌不能客户Id和门店Id同时为空！");
        }
        boolean success = customBrandMapper.insert(customBrand) > 0;
        if (success) {
            return CommonResult.success(customBrand.getId(), "新增品牌成功！");
        } else {
            return CommonResult.failed("新增品牌失败！");
        }
    }

    @Override
    public CommonResult<?> update(CustomBrand customBrand) {
        if (customBrand == null || customBrand.getId() == null) {
            return CommonResult.failed("更新品牌不能为空！");
        }

        boolean success = customBrandMapper.updateById(customBrand) > 0;
        if (success) {
            return CommonResult.success(customBrand.getId(), "更新品牌失成功！");
        } else {
            return CommonResult.failed("更新品牌失败！");
        }
    }

    @Override
    public CommonResult<?> delete(Long id) {
        if (id == null) {
            return CommonResult.failed("删除品牌Id不能为空！");
        }
        boolean success = customBrandMapper.deleteById(id) > 0;
        if (success) {
            return CommonResult.success("删除品牌失成功！");
        } else {
            return CommonResult.failed("删除品牌失败！");
        }
    }

    @Override
    public CommonResult<CustomBrand> getById(Long id) {
        if (id == null) {
            return CommonResult.failed("获取品牌Id不能为空！");
        }
        CustomBrand customBrand = customBrandMapper.selectById(id);
        if (customBrand != null) {
            return CommonResult.success(customBrand, "获取品牌成功！");
        } else {
            return CommonResult.failed("获取品牌失败！");
        }
    }

    @Override
    public CommonResult<List<CustomBrand>> listAll() {
        List<CustomBrand> list = customBrandMapper.selectList(null);
        if (!list.isEmpty()) {
            return CommonResult.success(list, "获取品牌成功！");
        } else {
            return CommonResult.failed("获取品牌失败！");
        }
    }

    @Override
    public CommonResult<List<CustomBrand>> listByCustomId(Long customId) {
        LambdaQueryWrapper<CustomBrand> query = new LambdaQueryWrapper<>();
        query.eq(CustomBrand::getCustomerId, customId);
        List<CustomBrand> list = customBrandMapper.selectList(query);

        if (!list.isEmpty()) {
            return CommonResult.success(list, "获取品牌成功！");
        } else {
            return CommonResult.failed("获取品牌失败！");
        }
    }

    @Override
    public CommonResult<List<CustomBrand>> listByStatus(Integer status) {
        LambdaQueryWrapper<CustomBrand> query = new LambdaQueryWrapper<>();
        query.eq(CustomBrand::getStatus, status);
        List<CustomBrand> list = customBrandMapper.selectList(query);
        if (!list.isEmpty()) {
            return CommonResult.success(list, "获取品牌成功！");
        } else {
            return CommonResult.failed("获取品牌失败！");
        }
    }
}